/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package qlvbdq.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import qlvbdq.dconnection.SQLServerConnect;
import qlvbdq.dto.UserDTO;
import qlvbdq.dto.VaitroDTO;

/**
 *
 * @author tuan
 */
public class UserDAO {
    
    public UserDAO(){
    }

    public ArrayList<UserDTO> GetAllUserFromDatabase() throws SQLException {
        ArrayList<UserDTO> listUser = new ArrayList<>();
        ResultSet result = SQLServerConnect.getInstance().getDataFromStore("GetAllDataOfUser", null);
        UserDTO user = new UserDTO();
        while (result.next()) {
            user.setUserName(result.getString("USERNAME"));
            user.setPassword(result.getString("PASSWORDS"));
            user.setLoaiNguoiDung(result.getString("LOAINGUOIDUNG"));
            listUser.add(user);
        }
        return listUser;
    }
    
    public UserDTO getUserByUserNameFromDatabase(String UserName) throws SQLException {
        Object[] objs = { UserName };
        ResultSet result = SQLServerConnect.getInstance().getDataFromStore("GetUserByUserNameFromDatabase( ? )", objs);
        UserDTO user = new UserDTO();
        while (result.next()) {
            user.setUserName(result.getString("USERNAME"));
            user.setPassword(result.getString("PASSWORDS"));
            user.setLoaiNguoiDung(result.getString("LOAINGUOIDUNG"));
            
        }
        return user;
    }
    
    public ArrayList<VaitroDTO> getAllVaiTroByUserName(String UserName) throws SQLException {
        Object[] objs = { UserName };
        ArrayList<VaitroDTO> listVaitro = new ArrayList<>();
        ResultSet result = SQLServerConnect.getInstance().getDataFromStore("GetCTPhanQuyenByUserNameFromDatabase( ? )", objs);
        String maVaitro = null;
        while (result.next()) {
            maVaitro = result.getString("MAVAITRO");
        }
        Object[] objs2 = { maVaitro };
        result = SQLServerConnect.getInstance().getDataFromStore("GetVaiTroByMaVaiTroFromDatabase( ? )", objs2);
        VaitroDTO vaitro;
        while (result.next()) {
            vaitro = new VaitroDTO(result.getString("MVAITRO"), result.getString("TENVAITRO"));
            listVaitro.add(vaitro);
        }
        return listVaitro;
    }
    //update 1 user
    public int updateUserToDatabase(UserDTO user) {
        Object[] objs = { user.getUserName(), user.getPassword(), user.getLoaiNguoiDung()};
        return SQLServerConnect.getInstance().updateDataToDatabase("UpdateUserToDatabase(?, ?, ?)", objs);
    }
    
    //delete 1 user
    public int deleteUserToDatabase(UserDTO user) {
        Object[] objs = { user.getUserName() };
        return SQLServerConnect.getInstance().updateDataToDatabase("DeleteUserToDatabase(?)", objs);
    }
    
    public int InsertUserToDatabase(UserDTO user) {
        Object[] objs = { user.getUserName(), user.getPassword(), user.getLoaiNguoiDung()};
        return SQLServerConnect.getInstance().updateDataToDatabase("InsertUserToDatabase(?, ?, ?)", objs);
    }
    
    
    
}
